Welcome back. One metaphor I find helpful is to think of a function as a machine.
It may take some inputs, the parameters,
and then when you run it,
that's the shaking that you see in this animation,
it can produce an output.
So, let's look at how to make a function return a value.
So, here's a definition of the square function.
Notice line three, we have
a special word 'return' and then afterwards, any Python expression.
In this case, just a reference to the variable y.
The value of that variable is some Python object,
and that's the value that will be returned from the function.
Let's take a look at line six,
we're invoking the function square,
we're passing in an input, an argument,
that's going to be bound to the first formal parameter name x,
and then we'll execute this function,
we're going to multiply whatever x is,
x is going to be whatever toSquare was,
and we'll multiply it by itself on line two and assign it to the variable y,
we're going to return a value and whatever is returned,
becomes the value of the entire expression,
so that is what is going to be bound to the variable result.
Note that the word result here is not any special word,
that's just a variable name.
Return is a special word,
that means to return a value from the function.
So, let's see that in code lens.
So, we first define the square function,
then we get toSquare is bound to the value 10,
that happened on line five,
and then on line six,
we are going to invoke the square function.
So behind the scenes,
we're going to get an assignment that
the variable x is going to get whatever value toSquare had,
so toSquare was 10,
and so x is going to have the value 10.
We then execute the code that's inside the square function,
we get y is 10 times 10, or a 100,
and then we return y, so,
the value of this entire expression square of toSquare becomes 100,
and that is the value that gets assigned to the variable square result.
Now, if you were paying close attention there,
you will have seen that when we finish the execution of the square function,
some of the variables disappeared over here,
we had them, and then they disappeared when we finished.
We're going to talk more about that in later screen casts.
So hold on for some of those details.
Now, if a function doesn't have a return statement,
it will automatically return a special value called 'none'.
Let's see that, here's an example illustrating a common confusion for students,
printing a value doesn't return that value,
so on line three,
we really should have a return statement.
In our definition of the square function,
we've calculated 10 times 10 or whatever it is and we should be returning it,
but instead we're printing it.
If we don't have a return statement at all,
in the function definition it says,
"If we had a statement at the end saying return the special value none."
So, let's see what's going to happen in this execution,
we define the function square,
we set toSquare to have the value 10.
Now we invoke the function,
x gets as its value.
Now, behind the scenes assignment x is getting whatever toSquare has,
so x gets the 10 toSquare was,
x has the value 10,
and we execute y gets the value of a 100,
and then we print y, so,
we can see in the output window, we've already printed y.
Well, we didn't return anything and so it's as if we have returned the value none,
and that means that square result is going to have as its value, none.
Now, you see here square result has the value none,
and that means that when we go to print, this formatted statement,
the result of something squared is
something we're going to get that the result of 10 squared is none,
rather than the result of 10 squared being 100.
What we really needed to do was not print that return y,
and then we would have gotten that the result of 10 squared is 100.
One other important thing about the return statement,
it interrupts the execution of the function,
no other code in the function executes after a return statement is executed.
If you've played the board game Monopoly,
it's like the go to jail move.
Do not pass gold, do not collect $200.
In our case, do not execute any more code inside
this function even if you're in the middle of a four-loop or an if statement,
return statement makes you skip the rest of the code in that function.
Here's an example, define this function weird,
not actually useful, but it's going to be very
illustrative of how this immediate return works.
So, lines one through five to find the function,
line six invokes it so when we invoke it,
and we get to line two,
we'll print out the word here,
and then we'll return the value five.
As soon as we return,
means we are not going to do anything with the remaining lines of code,
they are orphaned, we will not pass go,
we will not execute print there,
we will return the value five and x will have the value five,
and we will print five.
So, we do get the here printing,
we don't get there,
there never shows up in our output,
and the five is coming from line eight.
That's not a very useful function which is why I called it weird,
but let's look at a common programming pattern
where return does occur in the middle of a function,
and it's a good idea.
This function called longer than five returns a Boolean value, true or false?
It takes as an input,
it's one formal parameter called lists of names,
it's expecting there to be a list of strings.
If any of those strings has more than five characters,
the function returns true,
and otherwise it returns false.
So with list one,
all the name Sam, Tera, Sal, Amita,
they're all five characters or less returned
false because none of them have a name longer than five characters.
With lists two, both Lauren and Natalie are names that
have more than five characters and so longer than five will return true.
How does it work? Well, we loop through all of the names.
That's what's happening on lines two through five,
and if we ever get a long one,
if the current name is long,
then we will execute line four,
and we will return the value true.
As soon as we do that,
we're done with the four loop,
we're done with everything.
Do not pass go, don't collect $200,
get out of this function,
and just return the value true.
If however, we managed to get through the entire iteration
without ever exiting the function,
without ever executing line four,
then we will execute line six which says to return false.
So, the reason this logic works is that we're never going to get to
line six if any of the names are long.
We'll only get to line six if we've processed all of the names,
and none of them caused us to return true.
So, that means that none of the names were long,
and it's safe to return false.
Let's see this in code lens, we'll go step-by-step.
So, we define this function,
we invoke it the first time on the list one,
Sam, Tera, Sal, and Amita.
So behind the scenes,
we get list of names getting bound to that list,
and then we're going to start iterating through that list
of names and the first iteration,
our variable name is bound to Sam,
is it longer than five characters?
No, it's not. So, we go on and get a new value for name,
name is now bound to Tera,
Tera is not long,
Sal is not long,
Amita is also not long,
and now we've iterated through all the list of
names and we get to line six where it says,
"I guess none of them are long so let's return false" The second time we invoke it,
we get the other list of names and that when we iterate through,
Rey is not longer than five,
Ayo is not longer than five,
but Lauren is longer than five,
and so finally line four executes,
we return true, we never even get to look at Natalie.
But Lauren was long, and that's enough to know that one of them was too long,
and we get an answer of true.
So in summary, to return a value from a function,
we have the word return and then some expression that evaluates to a value.
In this case, the value false.
If we're in the middle of the function,
and we have a return,
we ignore the rest of the code in the function,
we exit out of the four-loop,
we exit out of the if,
we don't pass go, we don't collect $200,
we're just done with this function.
If, we have a function that never executes any return statement at all,
we will return the special value none.
The other thing to remember is that print is for people,
you can use it inside of a function and it'll generate an output in the output window,
but it doesn't cause anything to return from the function.
Return is for functions,
it doesn't print anything,
but it returns a value to the spot where the function was invoked. See you next time.